<?php

namespace app\common\controller;

use think\Controller;
use OSS\OssClient;
use OSS\Core\OssException;
use Carbon\Carbon;

class Upload extends Controller
{
    const accessKeyId = "gw9QdAPaIsYjR1Uu";
    const accessKeySecret = "dyOOXvGW0Q6bG05Agr7hXJCr5wdBDd";
    const endpoint = 'oss.shsytour.com';

    static $expire = 30;//超时时间

    protected $ossClient;
    protected $bucket = 'shsytour'; 
    protected $custom_dir = '';

    private static $_instance;

    public function __construct()
    {
        $this->configs = config('oss');
        if ($this->configs['bucket']) {
            $this->bucket = $this->configs['bucket'];
        }
        try {
            $ossClient = new OssClient($this->configs['accessKeyId'], $this->configs['accessKeySecret'], $this->configs['endpoint'], $this->configs['iscname']);
        } catch (OssException $e) {
            return null;
        }
        $this->ossClient = $ossClient;
    }

    public static function getInstance()
    {
        if (!(self::$_instance instanceof self)) {
            self::$_instance = new self;
        }
        return self::$_instance;
    }

    public function setBucket($bucket)
    {
        $this->bucket = $bucket;
        return $this;
    }
    /**
     * 设置自定义文件夹
     * @param string $custom_dir 文件夹路径
     */
    public function setCustomDir($custom_dir)
    {
        $this->custom_dir = $custom_dir;
        return $this;
    }
    /**
     * 获取自定义上传文件夹
     * @return string 文件夹路径
     */
    public function getCustomDir()
    {
        return $this->custom_dir;
    }
    /**
     * 上传文件到OSS
     * @param  [type] $file       要上传的文件
     * @param  string $custom_dir 自定义的上传文件夹
     * @return [type]             [description]
     */
    public function uploadFile($field)
    {
    	$file = request()->file($field);
        $savename = date('Ymd') . DIRECTORY_SEPARATOR . md5(microtime(true));
        $savename .= '.'.pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);

        if ($this->custom_dir) {
            $custom_dir = preg_replace('/(?:'.preg_quote(DIRECTORY_SEPARATOR, '/').')+$/u', '', $this->custom_dir).DIRECTORY_SEPARATOR;
        }

        $f['type'] = $file->getInfo('type');
        $f['filename'] = $file->getInfo('name');
        $f['filesize'] = $file->getInfo('size');
        $f['attachment'] = $savename;
        $f['url'] = (isset($custom_dir) ? $custom_dir : '').$savename;

        try{
            $this->ossClient->uploadFile($this->bucket, $f['url'], $file->getRealPath());
        } catch(OssException $e) {
            return false;
        }
        return $f;
    }
    public function deleteFile($object)
    {
        try {
            $this->ossClient->deleteObject($this->bucket, $object);
        } catch (OssException $e) {
            return false;
        }
        return true;
    }
    public function deleteFiles($objects)
    {
        if (!is_array($objects)) {
            $objects = array($objects);
        }
        try {
            $this->ossClient->deleteObjects($this->bucket, $objects);
        } catch (OssException $e) {
            return false;
        }
        return true;
    }
    public function getObjectUrl($object)
    {
        return $this->ossClient->signUrl($this->bucket, $object, self::$expire);
    }
    public function signUrl($bucket, $object)
    {
        //return $this->ossClient->signUrl($bucket, "a.file", "3600", "PUT");
        return $this->ossClient->signUrl($bucket, $object, self::$expire);
    }
}
